package com.express.controller.user;

import com.express.dto.PasswordChangeRequest;
import com.express.dto.RegisterDto;
import com.express.dto.UserDto;
import com.express.result.Result;
import com.express.service.IUsersService;
import com.express.vo.LoginVo;
import com.express.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户端")
public class UserController {

    @Autowired
    private IUsersService usersService;

    /**
     * 用户登录
     *
     * @param userDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<LoginVo> login(@RequestBody UserDto userDto) {
        log.info("用户登录：{}", userDto);
        LoginVo loginVo = usersService.login(userDto);
        return Result.success(loginVo);
    }

    /**
     * 注册
     *
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody RegisterDto registerDto) {
        log.info("用户注册：{}", registerDto);
        usersService.register(registerDto);
        return Result.success();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/getInfo")
    @ApiOperation("获取用户信息")
    public Result<UserInfoVo> getInfo() {
        UserInfoVo UserInfo = usersService.getInfo();
        log.info("获取用户信息:{}", UserInfo);
        return Result.success(UserInfo);
    }

    /**
     * 修改用户信息c
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping("/updateUserInfo")
    @ApiOperation("修改用户信息")
    public Result updateInfo(@RequestBody UserInfoVo userInfoVo) {
        log.info("修改用户信息:{}", userInfoVo);
        usersService.updateInfo(userInfoVo);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PostMapping("/change/password")
    @ApiOperation("修改用户密码")
    public Result changePassword(@RequestBody PasswordChangeRequest request) {
        log.info("修改密码:{}", request);
        usersService.updatePassword(request);
        return Result.success("密码修改成功，请重新登录");
    }
}
